package logs

import (
	"time"
	"fmt"
)

const (
	LevelAlter = iota
	LevelError
	LevelWarning
	LevelNotice
	LevelInformational
	LevelDebug
)

var levelPrefix = [LevelDebug+1]string{
	"[A] ",
	"[E] ",
	"[W] ",
	"[N] ",
	"[I] ",
	"[D] ",
}


type LoggerInterface interface {
	Init(file string) error
	WriteMsg(when time.Time, msg string) error
	Destroy()
	Flush()
}


type Logger struct {
	output LoggerInterface
}

func NewLogger(file string) *Logger {
	l := new(Logger)
	l.output = newFileLogger(file)
	if l.output == nil {
		return nil
	}
	return l
}


func (l *Logger) writeMsg(level int, msg string, v ...interface{})  {
	when := time.Now()
	if len(v) > 0 {
		msg = fmt.Sprintf(msg, v...)
	}

	msg = levelPrefix[level] + msg
	l.output.WriteMsg(when, msg)
}


func (l *Logger) Alter(format string, v ...interface{}) {
	l.writeMsg(LevelAlter, format, v...)
}


func (l *Logger) Error(format string, v ...interface{}) {
	l.writeMsg(LevelError, format, v...)
}


func (l *Logger) Warning(format string, v ...interface{}) {
	l.writeMsg(LevelWarning, format, v...)
}


func (l *Logger) Notice(format string, v ...interface{}) {
	l.writeMsg(LevelNotice, format, v...)
}


func (l *Logger) Debug(format string, v ...interface{}) {
	l.writeMsg(LevelDebug, format, v...)
}


func (l *Logger) Info(format string, v ...interface{}) {
	l.writeMsg(LevelInformational, format, v...)
}



